<!-- 置换专区订单列表 -->
<template>
	<view class="container">
		<uni-nav-bar fixed="true" statusBar="true" @clickLeft="backClick" color="#303133" leftIcon="back" background="#fff">
			<view class="searchBox">
				<icon class="searchIcon" type="search" :size='20' color='#999'></icon>
				<input class="topSearch" type="text" confirm-type="search" v-model="keys" placeholder="请输入搜索内容" @confirm="search" />
			</view>
		</uni-nav-bar>
		<view class="content">
			<view class="tabBox">
				<view v-for="(item,index) in tabList" :key="index" :class="[1==1?'tab':'',current == index?'active':'']" @tap="changeTab(index)">{{item}}</view>
			</view>
			<!-- 空白页 -->
			<empty v-if="emptyShow" :custom="true" src="../../static/images/no-orders.png" text="您还没有置换订单哦"></empty>

			<!-- 订单列表 -->
			<navigator v-if="current == 0" v-for="(items,index) in orderList" :key="index" :url="'/mall/permutation/perOrderDetail?id='+items.order_id"
			 hover-class="none">
				<view class="order-item">
					<view class="i-top b-b">
						<!-- <image class="seller_logo" :src="items.online_logo" mode=""></image> -->
						<text class="seller_name clamp">{{items.online_name}}</text>
						<text class="state">{{items._status._title}}</text>
					</view>

					<scroll-view v-if="items.cartInfo.length > 1" class="goods-box" scroll-x>
						<view v-for="(goodsItem, goodsIndex) in items.cartInfo" :key="goodsIndex" class="goods-item">
							<image v-if="goodsItem.productInfo.attrInfo" class="goods-img" :src="goodsItem.productInfo.attrInfo.image" mode="aspectFill"></image>
							<image v-else class="goods-img" :src="goodsItem.productInfo.image" mode="aspectFill"></image>
						</view>
					</scroll-view>
					<view v-if="items.cartInfo.length === 1" class="goods-box-single" v-for="(goodsItem, goodsIndex) in items.cartInfo"
					 :key="goodsIndex">
						<image class="goods-img" v-if="goodsItem.productInfo.attrInfo" :src="goodsItem.productInfo.attrInfo.image" mode="aspectFill"
						 @tap.stop="goGoods(goodsItem.product_id)"></image>
						<image class="goods-img" v-else :src="goodsItem.productInfo.image" mode="aspectFill"></image>
						<view class="right">
							<text class="title clamp2">{{goodsItem.productInfo.store_name}}</text>
							<view class="attr-box">
								<text v-if="goodsItem.productInfo.attrInfo">规格：{{goodsItem.productInfo.attrInfo.suk}}</text>
								<text>数量：x {{goodsItem.cart_num}}</text>
							</view>
						</view>
					</view>
					<!-- 待支付 -->
					<view class="action-box b-t" v-if="items.paid == 0">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系商家</button>
						<button class="action-btn" @tap.stop="checkMoney(items)">立即支付</button>
						<text class="price">{{items.pay_price}}</text>
					</view>
					<!-- 待发货 -->
					<view class="action-box b-t" v-if="items.status == 0&&items.paid == 1">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系商家</button>
						<button class="action-btn" @tap.stop="refund(items)">申请退款</button>
						<text class="price">{{items.pay_price}}</text>
					</view>
					<!-- 待收货 -->
					<view class="action-box b-t" v-if="items.status == 1">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系商家</button>
						<button class="action-btn" @tap.stop="express(items.order_id)">查看物流</button>
						<button class="action-btn" @tap.stop="queren(items.order_id)">确认收货</button>
						<text class="price">{{items.pay_price}}</text>
					</view>
					<view class="action-box b-t" v-if="items.status == 2">
						<button class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系商家</button>
						<button class="action-btn" @tap.stop="express(items.order_id)">查看物流</button>
						<text class="price">{{items.pay_price}}</text>
					</view>
				</view>
			</navigator>
			<!-- 退货列表 -->
			<navigator  v-if="current == 1" v-for="(items,index) in orderList" :key="index" :url="'/mall/order/refund/refundStep?id='+items.id + '&type=' + items.type"
			 hover-class="none">
				<view class="order-item">
					<!-- 店铺名称 -->
					<view class="i-top b-b" @tap.stop="goStore(items.seller_id)">
						<image class="seller_logo" :src="items.sellerInfo.online_logo" mode=""></image>
						<text class="seller_name clamp">{{items.sellerInfo.online_name}}</text>
						<text class="yticon icon-you"></text>
						<text class="state" v-if="items.status == 0">提交申请</text>
						<text class="state" v-if="items.type == 1&&[1].includes(Number(items.status))">商家同意</text>
						<text class="state" v-if="items.type == 0&&[1,6].includes(Number(items.status))">商家同意</text>
						<text class="state" v-if="items.status == 2">寄送商品</text>
						<text class="state" v-if="items.type == 1&&[3,6].includes(Number(items.status))">商家收货</text>
						<text class="state" v-if="items.status == 5">退款完成</text>
						<text class="state" v-if="items.status == -1">商家拒绝</text>
					</view>
			
					<scroll-view v-if="items.return_goods.length > 1" class="goods-box" scroll-x>
						<view v-for="(goodsItem, goodsIndex) in items.return_goods" :key="goodsIndex" class="goods-item" @tap.stop="goGoods(goodsItem.product_id)">
							<image v-if="goodsItem.attrInfo" class="goods-img" :src="goodsItem.attrInfo.image" mode="aspectFill"></image>
							<image v-else class="goods-img" :src="goodsItem.image" mode="aspectFill"></image>
						</view>
					</scroll-view>
					<view v-if="items.return_goods.length === 1" class="goods-box-single" v-for="(goodsItem, goodsIndex) in items.return_goods"
					 :key="goodsIndex">
						<image class="goods-img" v-if="goodsItem.attrInfo.length !== 0" :src="goodsItem.attrInfo.image" mode="aspectFill"
						 @tap.stop="goGoods(goodsItem.product_id)"></image>
						<image class="goods-img" v-else :src="goodsItem.image" mode="aspectFill" @tap.stop="goGoods(goodsItem.product_id)"></image>
						<view class="right">
							<text class="title clamp">{{goodsItem.store_name}}</text>
							<view class="attr-box">
								<text>{{goodsItem.attrInfo.suk||''}}</text>
								<text>x {{goodsItem.product_num}}</text>
							</view>
							<!-- <text class="price" v-if="goodsItem.attrInfo.length !== 0">{{goodsItem.attrInfo.price}}</text>
							<text class="price" v-else>{{goodsItem.price}}</text> -->
						</view>
					</view>
			
					<view class="price-box">
						共
						<text class="num">{{items.total_num}}</text>
						件商品 共退款
						<text class="price">{{items.refund_money}}</text>
						<view class="shell" v-if="items.refund_yellow_shell_num != 0">黄贝壳 <text>{{items.refund_yellow_shell_num}}</text></view>
						<view class="shell" v-if="items.refund_blue_shell_num != 0">蓝贝壳 <text>{{items.refund_blue_shell_num}}</text></view>
						<view class="shell" v-if="items.refund_red_shell_num != 0">红贝壳 <text>{{items.refund_red_shell_num}}</text></view>
					</view>
				</view>
			</navigator>
			<uni-load-more class="loadMore" iconType="circle" :status="loadStatus" :contentText="contentText"></uni-load-more>
		</view>
		<!-- app选择支付方式 -->
		<choosePay ref="choosePay" @confirm="confirm"></choosePay>
		<!-- 抵扣全部金额输入平台操作密码 -->
		<passkeyborad class="passkeyborad" :show="keyboradShow" @close="keyboradClose" @finish="finished"></passkeyborad>
	</view>
</template>

<script>
	import {
		rlmtorderList,
		rlmtorderTake,
		rlmtorderPay,
		rlmtreturnList
	} from '@/api/mall.js'
	import {
		checkPassword
	} from "@/api/user.js"
	import passkeyborad from '@/components/yzc-paykeyboard/yzc-paykeyboard.vue'
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty/empty";
	import wechat from '@/common/wechat.js'
	import choosePay from '@/components/choosePay/choosePay.vue'
	export default {
		components: {
			passkeyborad,
			uniNavBar,
			uniLoadMore,
			empty,
			choosePay
		},
		data() {
			return {
				tabList: ['置换订单', '退货退款'],
				current: 0,
				emptyShow: false,
				keys: '',
				orderList: [],
				limit: 6,
				page: 1,
				loadStatus: "loading",
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多了"
				},
				paytype: "weixin",
				provider: "wxpay",
				// #ifdef MP-WEIXIN
				payStatus: "routine",
				// #endif
				// #ifdef APP-PLUS
				payStatus: "app",
				// #endif
				// #ifdef H5
				payStatus: "weixin",
				// #endif
				keyboradShow: false,
				order_id: ''
			};
		},
		onShow(options) {
			this.current = 0
			this.orderList = []
			this.page = 1
			this.loadStatus = "loading"
			this.keys = ''
			this.emptyShow = false
			this.getOrderList()
		},
		onReachBottom() {
			if (this.loadStatus == "more") {
				this.loadStatus = "loading"
				this.getOrderList()
			}
		},
		methods: {
			// 返回
			backClick() {
				uni.navigateBack({

				})
			},
			//搜索
			search() {
				this.orderList = []
				this.page = 1
				this.loadStatus = "loading"
				this.emptyShow = false
				this.getOrderList()
			},
			changeTab(index) {
				this.current = index;
				this.orderList = []
				this.page = 1
				this.loadStatus = "loading"
				this.keys = ''
				this.emptyShow = false
				if (index == 0) {
					this.getOrderList()
				} else if (index == 1) {
					this.getReturnList()
				}
			},
			// 获取置换订单
			getOrderList() {
				rlmtorderList({
					search: this.keys,
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading()
					this.loadStatus = "more";
					this.orderList.push.apply(this.orderList, res.data);
					if (res.data.length < this.limit) this.loadStatus = "noMore"; //判断所有数据是否加载完成；
					this.page = this.page + 1;
					if (this.orderList.length == 0) {
						this.contentText.contentnomore = ''
						this.emptyShow = true
					} else {
						this.contentText.contentnomore = '没有更多了'
					}
				})
			},
			// 获取退款订单
			getReturnList() {
				rlmtreturnList({
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading()
					this.loadStatus = "more";
					this.orderList.push.apply(this.orderList, res.data);
					if (res.data.length < this.limit) this.loadStatus = "noMore"; //判断所有数据是否加载完成；
					this.page = this.page + 1;
					if (this.orderList.length == 0) {
						this.contentText.contentnomore = ''
						this.emptyShow = true
					} else {
						this.contentText.contentnomore = '没有更多了'
					}
				})
			},
			// 申请退款
			refund(items) {
				if (items.is_return == 1) {
					return uni.showToast({
						icon: "none",
						title: "订单正在退货中，请到 退换/售后 查看详情"
					})
				}
				uni.navigateTo({
					url: "/mall/order/refund/refund?oid=" + items.id + "&type=1"
				})
			},
			checkMoney(item) {
				this.order_id = item.order_id
				if (item.pay_postage > 0) {
					//#ifndef APP-PLUS
					this.goPay(item.order_id)
					//#endif
					//#ifdef APP-PLUS
					this.$refs.choosePay.show()
					//#endif
				} else {
					this.keyboradShow = true
				}
			},
			finished(password) {
				uni.showLoading({
					mask: true
				})
				checkPassword({
					password: password
				}, this.keyboradClose()).then(res => {
					this.goPay(this.order_id)
				})
			},
			confirm(e) {
				this.paytype = e.payType
				this.provider = e.provider
				this.goPay(this.order_id)
			},
			// 立即支付
			goPay(id) {
				uni.showLoading({
					mask: true
				})
				rlmtorderPay({
					uni: id,
					paytype: this.paytype,
					from: this.payStatus
				}).then(res => {
					// #ifdef MP-WEIXIN
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.result.jsConfig.timestamp,
						nonceStr: res.data.result.jsConfig.nonceStr,
						package: res.data.result.jsConfig.package,
						signType: res.data.result.jsConfig.signType,
						paySign: res.data.result.jsConfig.paySign,
						success: function(re) {
							uni.hideLoading()
							uni.navigateTo({
								url: "/mall/permutation/perOrderDetail?id=" + res.data.result.order_id
							})
						},
						fail(r) {
							console.log(r)
						}
					});
					// #endif
					// #ifdef APP-PLUS
					uni.requestPayment({
						provider: this.provider,
						orderInfo: res.data.result.jsConfig,
						success: (e) => {
							uni.navigateTo({
								url: "/mall/permutation/perOrderDetail?id=" + res.data.result.order_id
							})
						},
						fail: (e) => {
							console.log(e)
						}
					})
					// #endif
					//#ifdef H5
					// h5支付
					let url = "/mall/permutation/perOrderDetail?id=" + res.data.result.order_id
					wechat.pay(res.data.result.jsConfig, url, false)
					//#endif
				})
			},
			// 确认收货
			queren(id) {
				let _this = this
				uni.showLoading({
					mask: true
				})
				rlmtorderTake({
					uni: id
				}).then(res => {
					uni.hideLoading()
					_this.loadStatus = "loading";
					_this.page = 1;
					_this.orderList = [];
					_this.getOrderList();
				})
			},
			//联系商家
			phoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			// 查看物流
			express(id) {
				uni.navigateTo({
					url: "/mall/order/express?id=" + id
				})
			},
			keyboradClose() {
				this.keyboradShow = false
			},
		},
		onHide() {
			this.keyboradShow = false
		}
	}
</script>
<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.container {
		.searchBox {
			position: relative;

			.searchIcon {
				position: absolute;
				left: 20rpx;
				//#ifdef MP-WEIXIN
				top: 14rpx;
				//#endif
				//#ifdef APP-PLUS
				top: -14rpx;
				//#endif
				//#ifdef H5
				top: -10rpx;
				//#endif
				color: #9a9a9a;
			}

			.topSearch {
				background-color: #f8f8f8;
				//#ifdef MP-WEIXIN
				width: 420rpx;
				//#endif
				//#ifndef MP-WEIXIN
				width: 620rpx;
				//#endif
				height: 68rpx;
				padding-left: 80rpx;
				box-sizing: border-box;
				line-height: 68rpx;
				font-size: 24rpx;
				border-radius: 34rpx;
			}
		}

		.content {
			.tabBox {
				position: sticky;
				// #ifdef APP-PLUS
				top: calc(44px + var(--status-bar-height));
				// #endif
				// #ifdef H5
				top: 44px;
				// #endif
				display: flex;
				height: 88rpx;
				z-index: 99;
				background-color: #fff;
				box-shadow: 0px 6rpx 8rpx 0px rgba(0, 0, 0, 0.05);
			
				.tab {
					width: 50%;
					text-align: center;
					line-height: 88rpx;
					color: #484848;
					font-size: 30rpx;
				}
			
				.active {
					font-weight: bold;
					color: #484848;
					position: relative;
				}
			
				.active:after {
					content: '';
					position: absolute;
					height: 4rpx;
					width: 50rpx;
					border-radius: 2rpx;
					background-color: #FBC866;
					bottom: 10rpx;
					left: 50%;
					margin-left: -25rpx;
				}
			}
			.order-item {
				display: flex;
				flex-direction: column;
				margin: 20rpx 30rpx;
				padding: 0 20rpx;
				background: #fff;
				border-radius: 10rpx;

				.i-top {
					display: flex;
					align-items: center;
					height: 80rpx;
					padding-right: 30rpx;
					font-size: 28rpx;
					color: #303133;
					position: relative;

					.seller_logo {
						width: 40rpx;
						height: 40rpx;
						border-radius: 20rpx;
						margin-right: 20rpx;
					}

					.seller_name {
						max-width: 400rpx;
					}

					.icon-you {
						margin-top: 4rpx;
						margin-left: 10rpx;
						font-size: 24rpx;
					}

					.state {
						position: absolute;
						right: 0;
						top: 20rpx;
						color: #FBC866;
					}
				}

				/* 多条商品 */
				.goods-box {
					height: 120rpx;
					padding: 20rpx;
					width: 100%;
					margin-left: -20rpx;
					white-space: nowrap;
					background-color: #f8f8f8;

					.goods-item {
						width: 120rpx;
						height: 120rpx;
						display: inline-block;
						margin-right: 24rpx;
					}

					.goods-img {
						display: block;
						width: 100%;
						height: 100%;
					}
				}

				/* 单条商品 */
				.goods-box-single {
					display: flex;
					padding: 20rpx;
					margin-left: -20rpx;
					width: 100%;
					background-color: #f8f8f8;

					.goods-img {
						display: block;
						width: 120rpx;
						height: 120rpx;
					}

					.right {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						padding: 0 0 0 24rpx;
						overflow: hidden;

						.title {
							text-align: justify;
							font-size: 28rpx;
							color: #303133;
							height: 88rpx;
							line-height: 44rpx;
							overflow: hidden;
						}

						.attr-box {
							font-size: 24rpx;
							color: #909399;
							display: flex;

							text:nth-of-type(1) {
								margin-right: 50rpx;
							}
						}
					}
				}

				.action-box {
					display: flex;
					align-items: center;
					height: 100rpx;
					position: relative;
				}

				.action-btn {
					width: 130rpx;
					height: 50rpx;
					margin: 0;
					margin-right: 24rpx;
					padding: 0;
					text-align: center;
					line-height: 50rpx;
					font-size: 24rpx;
					color: #666;
					background: #fff;
					border-radius: 100px;


					&:after {
						border-radius: 100px;
					}
				}

				.price {
					position: absolute;
					right: 0;
					margin: 0;
					font-size: 36rpx;
					font-weight: bold;
					color: #333;
				}
				.price-box {
					display: flex;
					justify-content: flex-end;
					align-items: baseline;
					padding: 20rpx 0;
					font-size: 24rpx + 2rpx;
					color: #909399;
				
					.num {
						margin: 0 8rpx;
						color: #303133;
					}
				
					.price {
						position: relative;
						font-size: 32rpx;
						color: #FB3737;
						font-weight: bold;
				
						&:before {
							content: '￥';
							font-size: 24rpx;
							margin: 0 2rpx 0 8rpx;
						}
					}
					.shell{
						margin-left: 20rpx;
						text{
							margin-left: 10rpx;
							font-size: 32rpx;
							color: #FB3737;
							font-weight: bold;
						}
					}
				}
			}
		}
	}
</style>
